import init from '@feidao/web/framework/browser/d/component';
import { id, spaceid, systemid, refsystemid } from './config';
import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import { send_msg as urll } from './config';
import nodejs from '@feidao/web/atom/browser/msg/nodejs';
import fire from '@feidao/web/atom/browser/msg/fire';
import set from '@feidao/web/atom/browser/local/set';
import get from '@feidao/web/atom/browser/local/get';
const actions = {

	cmpt_show(fd: IFeidaoAiBrowserComponent, ...args: any[]) {

		fd.data.node.querySelector('[data-feidao-zjc="05"]').classList.remove('display-n');
		fd.data.node.classList.remove('display-n');
	},
	close_cmpt(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		fd.data.node.classList.add('display-n');
		const dom_class = fd.data.node.querySelectorAll<HTMLSpanElement>('.state1');
		Array.from(dom_class).forEach((item) => {
			item.classList.remove('bg-f4')
		});
	},

	change_role_no(fd: IFeidaoAiBrowserComponent, ...args: any[]) {

		const target_node = args[0].target;
		const role_no = target_node.getAttribute('data-feidao-role_no');
		const dom_class = fd.data.node.querySelectorAll<HTMLSpanElement>('.state1');
		if (!role_no) {
			return
		}
		Array.from(dom_class).forEach((item) => {
			item.classList.remove('bg-f4')
		});
		target_node.classList.add('bg-f4')
		set(fd, 'role_no', role_no);
		return;
	},
	async role_sure(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		const role_no = get(fd, 'role_no');
		if (!role_no) {
			fire('zjc-000001', 'ec-001', 'fail', '请选择角色');
			return;
		}
		const res = await nodejs(urll, 'web/change-role', { condition: { role_no } }, false, false);
		if (res['result']) {// 获取该角色下的首页菜单
			// const url = await nodejs(urll, 'user_jump', { data: { role_no } }, false, false);
			// if (url) {
			// 	(window as any).location.href = url;
			// } else {
			// 	fire('zjc-000001', 'ec-001', 'fail', '该角色下没有首页');
			// }
			(window as any).location.href = 'home.html';
		} else {
			fire('zjc-000001', 'ec-001', 'fail', '切换角色失败');
		}
	}
};

const s = {
	'cmpt_show': 'cmpt_show',
	'close_cmpt': 'close_cmpt',
	'role_sure': 'role_sure',
	'change_role_no': 'change_role_no'
};

export default function main(url: string, query: any) {
	return init(id, s, actions, url, query, { spaceid, systemid, refsystemid });
}
